package com.cci.kangdao.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;

import com.cci.kangdao.dao.model.NotifyRecord;

import java.util.List;
import java.util.Map;

@MapperScan
public interface NotifyRecordDYDao {

	//获取大运消息未读条数
	@Select("select count(*) from NotifyRecord_DY_T where ReceiverId = #{userId} and Flag = 0 and IsRead = 0 and SendType = 2")
	int getDYUnReadNumByUserId(@Param("userId") long userId);


	//用户App消息推送列表sql   看订单信息
	@Select("select workOrderNotify.OrderId,b.CreateTime as OrderIdCreateTime,e.ID as WorkOrderId,e.CreateTime as WorkOrderCreateTime,c.DValue as OrderStatus,f.DValue as WorkOrderStatus,\n" +
			"b.PlateNumbers,b.ESN,d.DValue as ServiceType,e.Distance,e.ServiceStationName,workOrderNotify.IsRead,workOrderNotify.SendTime,e.AppointmentTime\n" +
			"from (select OrderId,min(IsRead) IsRead,max(SendTime) SendTime from NotifyRecord_DY_T where ReceiverId = #{userid} and SendType = 2  group by OrderId) workOrderNotify\n" +
			"inner join AppointmentOrder_T b on workOrderNotify.OrderId = b.ID\n" +
			"inner join Dictionaries_T c on b.Status = c.DInt and c.DTableName = 'AppointmentOrderStatus' and c.Flag = 0\n" +
			"inner join Dictionaries_T d on b.ServiceType = d.DInt and d.DTableName = 'AppointmentServiceType' and d.Flag = 0\n" +
			"inner join AppointmentWorkOrder_T e on b.AppointmentWorkOrderId = e.ID\n" +
			"inner join Dictionaries_T f on e.Status = f.DInt and f.DTableName = 'AppointmentWorkOrderStatus' and f.Flag = 0\n" +
			"order by workOrderNotify.IsRead asc,workOrderNotify.SendTime desc")
	List<Map<String,Object>> getCustmerMsgList(@Param("userid")long userid);


	//服务站站长，信息员App消息推送列表sql   看工单信息
	@Select("select c.OrderId,c.ID as WorkOrderId,d.DValue as WorkOrderStatus,f.DValue as OrderStatus,c.CreateTime as WorkOrderCreateTime,b.CreateTime as OrderCreateTime,\n" +
			"b.PlateNumbers,b.ESN,c.AppointmentTime,e.DValue as ServiceType,b.Contact,b.ContactPhone,workOrderNotify.IsRead,workOrderNotify.SendTime\n" +
			"from (select OrderId,min(IsRead) IsRead,max(SendTime) SendTime from NotifyRecord_DY_T where ReceiverId = #{userid} and SendType = 2  group by OrderId) workOrderNotify\n" +
			"inner join AppointmentOrder_T b on workOrderNotify.OrderId = b.ID\n" +
			"inner join AppointmentWorkOrder_T c on b.ID = c.OrderId\n" +
			"inner join Dictionaries_T d on c.Status = d.DInt and d.DTableName = 'AppointmentWorkOrderStatus' and d.Flag = 0\n" +
			"inner join Dictionaries_T e on b.ServiceType = e.DInt and e.DTableName = 'AppointmentServiceType' and e.Flag = 0\n" +
			"inner join Dictionaries_T f on b.Status = f.DInt and f.DTableName = 'AppointmentOrderStatus' and f.Flag = 0\n" +
			"where c.ServiceStationId =#{serviceStationId}\n" +
			"order by workOrderNotify.IsRead asc,workOrderNotify.SendTime desc")
	List<Map<String,Object>> getServiceStationMsgList(@Param("userid")long userid,@Param("serviceStationId")long serviceStationId);


	//区域经理，400客服专员，区域leaderApp消息推送列表sql    看订单信息
	@Select("select c.OrderId,c.ID as WorkOrderId,d.DValue as WorkOrderStatus,g.DTableName as OrderStatus,b.CreateTime as OrderCreateTime,c.CreateTime as WorkOrderCreateTime,\n" +
			"b.PlateNumbers,c.AppointmentTime,e.DValue as ServiceType,b.Contact,b.ContactPhone,f.Phone,f.Telephone,workOrderNotify.IsRead,workOrderNotify.SendTime,c.ServiceStationName\n" +
			"from (select OrderId,min(IsRead) IsRead,max(SendTime) SendTime from NotifyRecord_DY_T where ReceiverId = #{userid} and SendType = 2  group by OrderId) workOrderNotify\n" +
			"inner join AppointmentOrder_T b on workOrderNotify.OrderId = b.ID\n" +
			"inner join AppointmentWorkOrder_T c on b.AppointmentWorkOrderId = c.ID\n" +
			"inner join Dictionaries_T d on c.Status = d.DInt and d.DTableName = 'AppointmentWorkOrderStatus' and d.Flag = 0\n" +
			"inner join Dictionaries_T e on b.ServiceType = e.DInt and e.DTableName = 'AppointmentServiceType' and e.Flag = 0\n" +
			"inner join ServiceStation_T f on c.ServiceStationId = f.ID\n" +
			"inner join Dictionaries_T g on b.Status = g.DInt and g.DTableName = 'AppointmentOrderStatus' and g.Flag = 0\n" +
			"order by workOrderNotify.IsRead asc,workOrderNotify.SendTime desc")
	List<Map<String,Object>> getLeaderMsgList(@Param("userid")long userid);

    //普通用户根据预约订单id,预约工单id,接收人获取消息列表
    @Select("select Content,SendTime,IsRead from NotifyRecord_DY_T where OrderId = #{orderId} and SendType = 2 and ReceiverId = #{receiverId} and Flag = 0\n" +
            "order by IsRead asc,SendTime desc")
    List<Map<String,Object>> getYHDYNotifyRecordList(@Param("orderId")long orderId,@Param("receiverId")long receiverId);

    //普通用户根据预约订单id把app推送消息标记已读
    @Update("update NotifyRecord_DY_T set IsRead = 1,Updator = #{userid},UpdateTime = #{updateTime} where OrderId = #{orderId} and SendType = 2 and ReceiverId = #{userid}")
    int markReadYHDYNotifyRecordByOrderId(@Param("userid")long userid,@Param("updateTime")String UpdateTime,@Param("orderId")long orderId);


	//服务站人员根据预约订单id,预约工单id,接收人获取消息列表
	@Select("select Content,SendTime,IsRead from NotifyRecord_DY_T where OrderId = #{orderId} and WorkOrderId = #{workOrderId} and SendType = 2 and ReceiverId = #{receiverId} and Flag = 0\n" +
			"order by IsRead asc,SendTime desc")
	List<Map<String,Object>> getDYNotifyRecordList(@Param("orderId")long orderId,@Param("workOrderId")long workOrderId,@Param("receiverId")long receiverId);

	//服务站人员根据预约订单id把app推送消息标记已读
    @Update("update NotifyRecord_DY_T set IsRead = 1,Updator = #{userid},UpdateTime = #{updateTime} where OrderId = #{orderId} and WorkOrderId = #{workOrderId} and SendType = 2 and ReceiverId = #{userid}")
	int markReadDYNotifyRecordByOrderId(@Param("userid")long userid,@Param("updateTime")String UpdateTime,@Param("orderId")long orderId,@Param("workOrderId")long workOrderId);

	//大运app推送消息一键已读
    @Update("update NotifyRecord_DY_T set IsRead = 1,Updator = #{userid},UpdateTime = #{updateTime} where IsRead = 0 and SendType = 2 and ReceiverId = #{userid}")
	int oneKeyReadDYNotifyRecord(@Param("userid")long userid,@Param("updateTime")String UpdateTime);
	
	
	int insertNotifyRecordSelective(NotifyRecord record);
	
	int insertBatchNotifyRecord(@Param("list") List<NotifyRecord> recordList);
}
